package com.app.twth.ui.cyclecount.sku;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityCyclecountSkuLpnBinding;
import com.app.twth.ui.cyclecount.CycleCountSku;
import com.app.twth.ui.cyclecount.CycleCountViewModel;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.List;

/**
 * @describe :
 */
public class CCSkuLPNActivity extends BaseActivity<ActivityCyclecountSkuLpnBinding, CycleCountViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 0;
    CycleCountSku cycleCountSku;
    private CycleCountSkuLPNAdapter adapter;
    List<CycleCountSku.InventoryListDTO> inventoryList;

    @Override
    protected void initListener() {
        viewModel.submitLiveData.observe(this, data -> {
            Intent intent = new Intent();
            intent.putExtra("data", data);
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.cycleCountSkuLiveData.observe(this, data -> {
            cycleCountSku = data;
            if (cycleCountSku.getIbLpnQty() == 0) {
                finish();
            }
            inventoryList = cycleCountSku.getInventoryList();
            adapter.setData(inventoryList);
        });
        viewModel.cycleCountSkuLpnLiveData.observe(this, data -> {
            binding.tvQty.requestFocus();
            binding.tvQty.setText("");
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "CycleCount(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext(getString(R.string.submit));
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        cycleCountSku = (CycleCountSku) bundle.getSerializable("data");
        inventoryList = cycleCountSku.getInventoryList();
        binding.tvSku.setText(cycleCountSku.getSku() + "(" + Utils.getStr2Int(cycleCountSku.getSkuQty()) + ")");
        binding.tvIbLpnQty.setText(Utils.getStr2Int(cycleCountSku.getIbLpnQty()));
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);
        binding.tvCode.setOnFocusChangeListener(onFocusChangeListener);
        binding.tvQty.setOnFocusChangeListener(onFocusChangeListener);
        binding.tvCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                binding.tvQty.setText("");
            }
        });

        adapter = new CycleCountSkuLPNAdapter(this, inventoryList, position -> {
            barcodeStr = inventoryList.get(position).getIbLpn();
            if (Utils.isEmpty(barcodeStr)) {
                return;
            }
            viewModel.cycleCountSkuInfo(barcodeStr, cycleCountSku.getSku());
        });
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager2);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                type = 0;
                setScan();
                break;
            case R.id.iv_qty:
                String cartonsQty = binding.tvQty.getText().toString();
                if (Utils.isEmpty(cartonsQty)) {
                } else if (Integer.parseInt(cartonsQty) > 1) {
                    binding.tvQty.setText((Integer.parseInt(cartonsQty) - 1) + "");
                } else {
                    binding.tvQty.setText("0");
                }
            case R.id.tv_reset:
                bottomLayoutBinding.tvHint.setText("");
                binding.tvCode.setText("");
                binding.tvQty.setText("");
                break;
            case R.id.tv_next:
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    ToastUtil.showToast(R.string.scan_inbound_lpn_lower);
                    return;
                }
                String strQty = binding.tvQty.getText().toString();
                if (Utils.isEmpty(strQty)) {
                    strQty = "0";
                }
                int qty = Integer.parseInt(strQty);
                if (qty == 0) {
                    ToastUtil.showToast("Please Scan +1");
                    return;
                }
                CycleCountSkuLPNQr adjustLpnQr = new CycleCountSkuLPNQr();
                adjustLpnQr.setIbLpn(barcodeStr);
                adjustLpnQr.setBarcode(cycleCountSku.getSku());
                adjustLpnQr.setQty(qty);
                viewModel.cycleCountSkuSubmit(adjustLpnQr);

                break;
        }
    }

    private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                type = 0;
                return;
            }

            switch (v.getId()) {
                case R.id.tv_code:
                    type = 0;
                    break;
                case R.id.tv_qty:
                    type = 1;
                    break;
            }
        }
    };

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                if (!Utils.isEmpty(barcodeStr)) {
                    viewModel.cycleCountSkuInfo(barcodeStr, cycleCountSku.getSku());
                }

            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();

        bottomLayoutBinding.tvHint.setText("");
        switch (type) {
            case 0:
                binding.tvCode.setText(barcodeStr);
                binding.tvQty.setText("");
                binding.tvCode.setText(barcodeStr);
                viewModel.cycleCountSkuInfo(barcodeStr, cycleCountSku.getSku());
                break;
            case 1:
                if (viewModel.cycleCountSkuLpnLiveData.getValue() == null) {
                    return;
                }
                if (viewModel.cycleCountSkuLpnLiveData.getValue().getBarcodeList() == null) {
                    return;
                }
                boolean isExist = false;
                for (String str : viewModel.cycleCountSkuLpnLiveData.getValue().getBarcodeList()) {
                    if (str.equalsIgnoreCase(barcodeStr)) {
                        isExist = true;
                    }
                }
                if (isExist) {
                    String ctn = binding.tvQty.getText().toString();
                    if (Utils.isEmpty(ctn)) {
                        binding.tvQty.setText(1 + "");
                    } else {
                        binding.tvQty.setText((1 + Integer.parseInt(ctn)) + "");
                    }
                }
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
